var SceneOnlineMap = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.sceneId = options.sceneId;
	this.globe = options.editor.globe;
	this.isOpen = false;
	this.dIndex;
	return this;
}

/**
 * 库
 */
SceneOnlineMap.prototype.library = function(id) {
	let _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.closeAll();
	layer.load(2);
	var content = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/library", { sceneId: _this.sceneId }, "html", null);
	layer.closeAll('loading');
	if (content) {
		layer.open({
			id: 'sceneOnlineMapLibrary',
			type: 1,
			anim: 5,
			title: '地图服务',
			area: '320px',
			content: content,
			shade: 0,
			offset: 'l',
			fixed: true,
			move: false,
			skin: 'layui-layer  layui-layer-adminRight layui-layer-city',
			success: function(layero, dIndex) {
				_this.dIndex = dIndex;
				_this.isOpen=true;
				$(layero).children('.layui-layer-content').css('overflow', 'hidden');
				$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
				_this.loadImageryList();
				_this.loadRoadList();
				_this.loadTerrainList();
				_this.loadAreaList();
				form.render();
			},
			end: function() { 
				_this.isOpen=false;
			}
		});
	}else{
		layer.msg(res?res.message:'异常！', {icon: 2});
	}
}

//底图
SceneOnlineMap.prototype.loadImageryList = function() {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/listData", {sceneId:_this.sceneId,classify:'imagery'}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(sceneMapImageryScript.innerHTML).render(res, function(html) {
			$('#select-scene-imagery-list').html(html);
			$(".OnlineMapList>.content>.item>.mapImge>.Thumbnail,.OnlineMapList>.content>.item>.mapImge>i").on('click', function() {
				  let  id=$(this).attr('id');
				  if( $("#item-"+id).hasClass("active")){
					  $("#item-"+id).removeClass("active")
					  _this.remove(id);
				  }else{
					  $("#item-"+id).addClass("active");
					  _this.add(id);
				  }
			})
		});
	}
}

//道路
SceneOnlineMap.prototype.loadRoadList = function() {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/listData", {sceneId:_this.sceneId,classify:'road'}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(sceneMapRoadScript.innerHTML).render(res, function(html) {
			$('#sceneMapRoad-source-item').html(html);
			form.on('switch(sceneMapRoadSwitch)', function(data){
				  if(data.elem.checked){
					  _this.add(data.elem.id);
				  }else{
					  _this.remove(data.elem.id);
				  }
			});
		});
	}
}
//地形
SceneOnlineMap.prototype.loadTerrainList = function() {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/listData", {sceneId:_this.sceneId,classify:'terrain'}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(sceneMapTerrainScript.innerHTML).render(res, function(html) {
			$('#sceneMapTerrain-source-item').html(html);
			form.on('switch(sceneMapTerrainSwitch)', function(data){
				  if(data.elem.checked){
					  _this.add(data.elem.id);
				  }else{
					  _this.remove(data.elem.id);
				  }
			});
		});
	}
}
//行政区
SceneOnlineMap.prototype.loadAreaList = function() {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/listData", {sceneId:_this.sceneId,classify:'area'}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(sceneMapAreaScript.innerHTML).render(res, function(html) {
			$('#sceneMapArea-source-item').html(html);
			form.on('switch(sceneMapAreaSwitch)', function(data){
				  if(data.elem.checked){
					  _this.add(data.elem.id);
				  }else{
					  _this.remove(data.elem.id);
				  }
			});
		});
	}
}

/**
 * 添加
 */
SceneOnlineMap.prototype.add=function(id){
	let _this=this;
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/add", {
		id:id,
		sceneId:_this.sceneId,
	}, "json", null);
	layer.closeAll('loading');
    if (res && 200==res.code) {
    	let data=res.data;
    	let param={
    		id:	data.id,
    		name:data.name,
    		code:data.code,
    		type:'0',
			thing:{
				auth:data.auth,
				provider:data.provider,
				type:data.type,
				classify:data.classify,
				serverType:data.serverType,
				options:data.options,
			}
    	}
    	_this.globe.render(param);
    } else {
        layer.msg(res?res.message:'系统异常!', {icon: 2});
    }
}

SceneOnlineMap.prototype.remove=function(id){
	let _this=this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/onlineMap/delete", {
		id:id,
		sceneId:_this.sceneId,
	}, "json", null);
	layer.closeAll('loading');
    if (res && 200==res.code) {
    	_this.globe.remove(res.data.id);
    } else {
        layer.msg(res?res.message:'系统异常!', {icon: 2});
    }
}


